package com.fw.ui.view.common.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.ui.view.common.ToggleControlPanel;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;

@Component
@Scope(SCOPE_PROTOTYPE)
public class ToggleControlPanelToggleButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 482478998438351054L;

    public ToggleControlPanel toggleControlPanel;

    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	Button btn = event.getButton();
	Boolean data = (Boolean) btn.getData();
	HorizontalLayout hlayout = (HorizontalLayout) toggleControlPanel.getTogglePanel().getContent();
	if (data != null && data.booleanValue()) {
	    btn.setCaption("Click to Display Search Criteria ... ");
	    hlayout.setVisible(false);
	    toggleControlPanel.getToggleButton().setVisible(false);
	    event.getButton().setData(new Boolean(false));
	} else {
	    btn.setCaption("Click to Hide Search Criteria ... ");
	    hlayout.setVisible(true);
	    toggleControlPanel.getToggleButton().setVisible(true);
	    event.getButton().setData(new Boolean(true));
	}
	return null;
    }

    @Override
    public void setBasedComponent(com.vaadin.ui.Component basedComponent) {
	this.toggleControlPanel = (ToggleControlPanel) basedComponent;
    }

    @Override
    public boolean validate(ClickEvent event) throws Exception {
	return true;
    }

}
